package com.xgq.drink.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgq.drink.entity.Brand;
import com.xgq.drink.entity.ChatContact;
import com.xgq.drink.entity.R;
import com.xgq.drink.entity.User;
import com.xgq.drink.mapper.BrandMapper;
import com.xgq.drink.mapper.ChatContactMapper;
import com.xgq.drink.mapper.UserMapper;
import com.xgq.drink.service.IChatContactService;
import com.xgq.drink.utils.RUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ChatContactServiceImpl extends ServiceImpl<ChatContactMapper, ChatContact> implements IChatContactService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BrandMapper brandMapper;
    // 获取用户全部聊天对象
    public R getAllContacts(Long userId) {
        List<ChatContact> contacts = this.lambdaQuery()
                .eq(ChatContact::getUserId, userId)
                .orderByDesc(ChatContact::getUpdateTime)
                .list();
        // 补充用户/品牌信息
        contacts.forEach(contact -> {
            String targetId = String.valueOf(contact.getTargetId());
            if (targetId.startsWith("1000")) { // 品牌用户
                String modifiedTargetId = targetId.substring(4);
                Brand brand = brandMapper.selectById(modifiedTargetId);
                if (brand != null) {
                    contact.setName(brand.getBrandName());
                    contact.setAvatar(brand.getBrandLogoUrl());
                }
                System.out.println("查询列表Brand: " + brand);
            } else { // 普通用户
                User user = userMapper.selectById(contact.getTargetId());
                if (user != null) {
                    contact.setName(user.getUsername());
                    contact.setAvatar(user.getAvatarUrl());
                }
            }
        });
        return RUtils.success("OK",contacts);
    }
    // 批量删除聊天对象
    public R batchDeleteContacts(Long userId, List<Long> targetIds) {
        boolean success = this.remove(new LambdaQueryWrapper<ChatContact>()
                .eq(ChatContact::getUserId, userId)
                .in(ChatContact::getTargetId, targetIds));
        return success ? RUtils.success("删除成功") : RUtils.fail("删除失败");
    }

    // 清空未读消息数
    public R clearUnreadCount(Long userId, Long targetId) {
        boolean success = this.update(new LambdaUpdateWrapper<ChatContact>()
                .eq(ChatContact::getUserId, userId)
                .eq(ChatContact::getTargetId, targetId)
                .set(ChatContact::getUnreadCount, 0));
        return success ? RUtils.success("已清空未读") : RUtils.fail("操作失败");
    }

    // 创建/更新聊天对象
    public R createOrUpdateContact(ChatContact contact) {
        this.saveOrUpdate(contact, new LambdaUpdateWrapper<ChatContact>()
                .eq(ChatContact::getUserId, contact.getUserId())
                .eq(ChatContact::getTargetId, contact.getTargetId()));
        return RUtils.success("操作成功", contact);
    }

    // 修改创建时间
    public R updateCreateTime(Long contactId, LocalDateTime newTime) {
        boolean success = this.update(new LambdaUpdateWrapper<ChatContact>()
                .eq(ChatContact::getId, contactId)
                .set(ChatContact::getCreateTime, newTime));
        return success ? RUtils.success("时间更新成功") : RUtils.fail("更新失败");
    }
}
